// 对user操作的model
import React from 'react';
import {Model, Effect} from 'dva-core-ts';
import {Reducer} from 'redux';
import axios from 'axios';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Toast from 'react-native-root-toast';
export interface IRecommends {
  _id:string,
  recommend_title: string, // 拍摄套餐的标题
  recommend_detail: string, // 拍摄套餐详情
  recommend_images: Images[], // 套餐图片
  recommend_price: number, // 拍摄价格
  recommend_time: number, // 拍摄时长 小时制
  owner_id: string, // 所属者id，users的obejct_id 64087e1353452635119925be liang 123456只有摄影师身份能发布套餐
}
export interface Images {
  rid: string;
  r_url: any;
  
}
export interface RecommendsModelState {
  recommends?: IRecommends[];
  recommendsByType:IRecommends[];
}
const RECOMMEND_URL = '/recommends/list';
const RECOMMENDTYPE_URL = '/recommendsByType/list';
const ADDRECOMMEND_URL = '/addRecommend';
const DELETERECOMMEND_URL = '/deleteRecommend'
interface RecommendsModel extends Model {
  namespace: 'recommends';
  state?: RecommendsModelState;
  reducers?: {
    updateState: Reducer<IRecommends>;
  };
  effects?: {
    getRecommendsList:Effect;
    getRecommendsListByType:Effect;
    addRecommend:Effect;
    deleteRecommend:Effect;
  };
}
const initValue = {
  recommends:[],
  recommendsByType:[],
};
const recommends: RecommendsModel = {
  namespace: 'recommends',
  state: initValue,
  reducers: {
    updateState: (state, {payload}) => {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *getRecommendsList({payload}, {call, put, select}): any {
      const res = yield call(axios.get, RECOMMEND_URL);
      console.log("进来了res",res)
      if (res.data.code === 200) {
        console.log("推荐数据",res.data.data)
        yield put({
          type: 'updateState',
          payload: {
            recommends: res.data.data,
          },
        });
        console.log("callback && typeof callback === 'function'",payload.callback)
        if (payload.callback && typeof payload.callback === 'function') {
          payload.callback(res.data.data);
        }
      }
    },
    // 按照类型进行搜索
    *getRecommendsListByType({payload}, {call, put, select}): any {
      console.log('photographer_name',payload.type)
      const res = yield call(axios.get, RECOMMENDTYPE_URL,{
        params:{type:payload.type}
      });
      console.log("按照type获取推荐数据res",res)
      if (res.data.code === 200) {
        console.log("推荐分类数据",res.data.data)
        yield put({
          type: 'updateState',
          payload: {
            recommendsByType: res.data.data,
          },
        });
      }
    },
    *addRecommend({payload},{call,put}):any {
      console.log('发布推荐的数据', payload.formData);
      // 处理formData数据
      const formData = new FormData();
      formData.append('owner_id', payload.formData.owner_id);
      formData.append('recommend_title', payload.formData.recommend_title);
      formData.append('recommend_detail', payload.formData.recommend_detail);
      formData.append('recommend_price', payload.formData.recommend_price);
      formData.append('recommend_time', payload.formData.recommend_time);
      payload.formData.images && payload.formData.images.forEach((image:any) => {
        const file = {
          uri: image.uri,
          type: 'image/jpeg',
          name: image.uri.split('/').pop()
        };
        formData.append('images', file);
      });
      console.log('发布推荐的formData之后的数据', formData);
      const res = yield call(axios.post, ADDRECOMMEND_URL, formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      });
      console.log('发布推荐的接口返回的数据', res);
      if (res.data.code === 200) {
        if (payload.callback && typeof payload.callback === 'function') {
          payload.callback();
        }
      } else {
        Toast.show('发布推荐失败', {
          position: Toast.positions.CENTER,
          duration: Toast.durations.LONG,
          shadow: true,
          animation: true,
        });
      }
    },
    *deleteRecommend({payload},{call,put}):any {
      console.log('删除套餐的数据',payload.formData);
      // 处理formData数据
      const formData = new FormData();
      formData.append('recommend_id', payload.formData.recommend_id);
      console.log("删除推荐转化成formData之后的数据",formData)
      const res = yield call(axios.post,DELETERECOMMEND_URL,formData,{
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      });
      console.log('删除推荐接口返回的数据',res);
      if(res.data.code === 200 ){
        if(payload.callback && typeof payload.callback === "function") {
          payload.callback()
        }
      } else {
        Toast.show('删除套餐失败', {
          position: Toast.positions.CENTER,
          duration: Toast.durations.LONG,
          shadow: true,
          animation: true,
        });
      }
    },
  },
};
export default recommends;